package com.treeman.navi;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.treeman.navi.map.route.Route;

public class RouteListViewActivity extends ListActivity
{
	public final static String TAG = "RouteListViewActivity";

	private Context mContext;

	private int mRequestCode;

	private CustomAdapter mCustomAdapter;

	private Route mRoute;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// View
		setContentView(R.layout.route_listview);
		Log.v(MainActivity.APPTAG, "-> SHOW ROUTE LIST VIEW ");

		mContext = getApplicationContext();

		try
		{
			// get params
			Bundle bun = this.getIntent().getExtras();

			mRequestCode = bun.getInt("requestCode");

			// set data

			mCustomAdapter = new CustomAdapter();
			if (MainActivity.mRoute != null)
			{
				mRoute = MainActivity.mRoute;

				for (int i = 0; i < mRoute.getPointsCount() - 1; i++)
				{
					mCustomAdapter.addItem(mRoute.getPoint(i).getName(), mRoute.getPoint(i).getDescription());
				}
			}

			setListAdapter(mCustomAdapter);
		} catch (Exception ex)
		{
			Log.e(MainActivity.APPTAG, TAG + " Error : ", ex);
		}
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if ((keyCode == KeyEvent.KEYCODE_BACK))
		{
			try
			{
				// showToastMessage("DODANO MARKER");

				Bundle bundle = new Bundle();

				bundle.putString("info", "list view close");

				Intent i = new Intent();

				i.putExtras(bundle);

				setResult(MainActivity.MENU_ROUTE_LIST_VIEW_SHOW_RESULT, i);

				finish();
			} catch (Exception ex)
			{
				Log.e(MainActivity.APPTAG, TAG + " Error : ", ex);
			}
		}
		return super.onKeyDown(keyCode, event);
	}


	@Override
	protected void onDestroy()
	{
		Log.v(MainActivity.APPTAG, "-> CLOSE ROUTE LIST VIEW ");
		super.onDestroy();
	}


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);

	}

	// ==============================================================
	// ==============================================================
	// ==============================================================

	protected class CustomAdapter extends BaseAdapter
	{
		private static final int TYPE_ITEM = 0;

		private LayoutInflater mInflater;

		private ArrayList<String[]> mData = new ArrayList<String[]>();


		public CustomAdapter()
		{
			mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}


		public void addItem(String name, String description)
		{
			String[] temp = new String[] { name, description };
			mData.add(temp);
			notifyDataSetChanged();
		}


		@Override
		public int getCount()
		{
			return mData.size();
		}


		@Override
		public Object getItem(int position)
		{
			return mData.get(position);
		}


		@Override
		public long getItemId(int position)
		{
			return position;
		}


		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			try
			{
				ViewHolder holder = null;
				int type = getItemViewType(position);

				if (convertView == null)
				{
					holder = new ViewHolder();
					switch ( type )
					{
						case TYPE_ITEM :
							convertView = mInflater.inflate(R.layout.listviewitem, null);
							holder.text = (TextView) convertView.findViewById(R.id.listViewItemText);
							holder.description = (TextView) convertView.findViewById(R.id.listViewItemDescription);
							holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.linearLayoutRouteListView);
							break;
					}
					convertView.setTag(holder);

				} else
				{
					holder = (ViewHolder) convertView.getTag();
				}

				convertView.setOnTouchListener(new OnTouchListener() {

					@Override
					public boolean onTouch(View v, MotionEvent event)
					{
						if (event.getAction() == MotionEvent.ACTION_DOWN)
						{
							ViewHolder vh = (ViewHolder) v.getTag();

							vh.linearLayout.setBackgroundColor(Color.parseColor(RouteListViewActivity.this.getString(R.color.orange)));
						}
						if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
						{
							ViewHolder vh = (ViewHolder) v.getTag();

							vh.linearLayout.setBackgroundColor(Color.parseColor(RouteListViewActivity.this.getString(R.color.white)));
						}
						return false;
					}
				});

				convertView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v)
					{
					}
				});

				holder.text.setText(mData.get(position)[0]);
				holder.description.setText(mData.get(position)[1]);
				return convertView;
			} catch (Exception ex)
			{
				Log.e(MainActivity.APPTAG, TAG + " Error : ", ex);
			}
			return null;
		}

	}

	public static class ViewHolder
	{
		public TextView text;

		public TextView description;

		public LinearLayout linearLayout;
	}

}
